#include "HardWare_LED.h"

/**
 * @brief 初始化
 *
 */
void HardWare_LED_Init(void)
{
    // 开启时钟使能
     RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
    // 开启 1 功能
    GPIOA->CRL |= GPIO_CRL_MODE0;
    GPIOA->CRL &= ~GPIO_CRL_CNF0;

    // 开启2
    GPIOA->CRL |= GPIO_CRL_MODE1;
    GPIOA->CRL &= ~GPIO_CRL_CNF1;

    // 开启8
    GPIOA->CRH |= GPIO_CRH_MODE8;
    GPIOA->CRH &= ~GPIO_CRH_CNF8;
    GPIOA->ODR |= (LED1 | LED2 | LED3);
}

/**
 * @brief 打开OLED
 *
 * @param led
 */
void HardWare_LED_On(uint16_t led)
{
    GPIOA->ODR &= ~led;
}

/**
 * @brief 关闭LED
 *
 * @param led
 */
void HardWare_LED_Off(uint16_t led)
{
    GPIOA->ODR |= led;
}

/**
 * @brief 全部打开
 *
 * @param led_array
 * @param led_lenth
 */
void HardWare_LED_AllOn(uint16_t *led_array, uint8_t led_lenth)
{
    for (int8_t i = 0; i < led_lenth; i++) {
        HardWare_LED_On(led_array[i]);
    }
}

/**
 * @brief 全部关闭
 *
 * @param led_array
 * @param led_lenth
 */
void HardWare_LED_AllOff(uint16_t *led_array, uint8_t led_lenth)
{
    for (int8_t i = 0; i < led_lenth; i++) {
        HardWare_LED_Off(led_array[i]);
    }
}

/**
 * @brief 反转
 *
 * @param led
 */
void HardWare_LED_Toggle(uint16_t led)
{
    // 为 1 则关闭
    if (GPIOA->IDR & led) {
        GPIOA->ODR &= ~led;
    } else {
        GPIOA->ODR |= led;
    }
}
